package c2.cross.business.guildwar.core;

import c2.cross.business.base.CrossSubModule;
import c2.cross.business.guildwar.GuildWarModule;
import cate.game.pvp.guildwar.data.GWInnerMsg;
import cate.game.pvp.guildwar.data.msg.*;
import cate.game.pvp.guildwar.data.rank.GWGuildRank;
import cate.game.pvp.guildwar.data.rank.GWRoleRank;
import cp.solution.rpc.RpcMsgHead;
import easy.java.dev.note.NoteClass;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@NoteClass("公会战-消息接受模块")
public class GWCrossReceiveModule extends CrossSubModule<GuildWarModule> {

    @Override
    public void initDependent() throws Exception {
        super.initDependent();
        useRpcRecv().onFireAndForgetTemplate(GWInnerMsg.GAME_CONNECT, Integer.class, this::onGameConnect);

        useRpcRecv().onRequestResponseTemplate(GWInnerMsg.SIGN, GWSignInnerReq.class, this::handleSign);

        useRpcRecv().onFireAndForgetTemplate(GWInnerMsg.RECORD_ROLE_RANK, GWRoleRankRecordInnerReq.class, this::handleRecordRole);
        useRpcRecv().onRequestResponseTemplate(GWInnerMsg.RECORD_GUILD_RANK, GWGuildRankRecordInnerReq.class, this::handleRecordGuild);

        useRpcRecv().onRequestResponseTemplate(GWInnerMsg.GET_GUILD_RANK, String.class,this::handleGetGuildList);
        useRpcRecv().onRequestResponseTemplate(GWInnerMsg.GET_ROLE_RANK, String.class,this::handleGetRoleList);
        useRpcRecv().onRequestResponseTemplate(GWInnerMsg.GET_CHAMPION, Void.class,this::handleGetChampion);


        useRpcRecv().onFireAndForgetTemplate(GWInnerMsg.ADMIRE_CHAMPION, Integer.class, this::handleAdmire);
    }

    /**
     * 游戏服连接
     *
     * @param head  头信息
     * @param srvId 服务器ID
     */
    public void onGameConnect(RpcMsgHead head, int srvId) {
        parent.onGameConnect(srvId);
    }


    /**
     * 处理报名信息
     * @param head
     * @param req
     * @return
     */
    public boolean handleSign(RpcMsgHead head, GWSignInnerReq req) {
        return parent.handleSign(req);
    }

    /**
     * 记录玩家数据
     * @param head
     * @param req
     */
    public void handleRecordRole(RpcMsgHead head, GWRoleRankRecordInnerReq req) {
        parent.handleRecordRole(req);
    }

    /**
     * 记录公会排行榜数据
     * @param head
     * @param req
     * @return
     */
    public boolean handleRecordGuild(RpcMsgHead head, GWGuildRankRecordInnerReq req) {
        parent.handleRecordGuild(req);
        return true;
    }


    public GWGuildRankInnerResp handleGetGuildList(RpcMsgHead head,String guildUid){
        GWGuildRankInnerResp resp = new GWGuildRankInnerResp();
        int myRank = parent.guildRank.getRankIndex(guildUid);
        GWGuildRank my = parent.guildRank.findRankByUid(guildUid);
        resp.myRank = myRank;
        resp.my = my;
        resp.list = parent.guildRank.getCopyList();
        return resp;
    }

    public GWRoleRankInnerResp handleGetRoleList(RpcMsgHead head, String roleUid){
        GWRoleRankInnerResp resp = new GWRoleRankInnerResp();
        int myRank = parent.roleRank.getRankIndex(roleUid);
        GWRoleRank my = parent.roleRank.findRankByUid(roleUid);
        resp.myRank = myRank;
        resp.my = my;
        resp.list = parent.roleRank.getList();
        return resp;
    }

    public GWChampionInnerResp handleGetChampion(RpcMsgHead head, Void v) {
        return parent.getChampion();
    }

    public void handleAdmire(RpcMsgHead head, int season) {
        parent.admire(season);
    }

}
